package com.cskaoyan.controller.front;

import com.cskaoyan.bean.bo.front.cartbo.*;
import com.cskaoyan.bean.vo.back.BaseRespVo;
import com.cskaoyan.bean.vo.front.cartvo.CartCheckoutVO;
import com.cskaoyan.bean.vo.front.cartvo.CartIndexVO;
import com.cskaoyan.service.front.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 12-江涛
 * @since 2022/10/13  23:29
 */
@RestController
@RequestMapping("wx/cart")
public class CartController {

    @Autowired
    CartService cartService;


    /*
     * 购物车主页  （获取列表、购物商品数量、总价格（所有商品以及勾选商品））
     * http://182.92.235.201:8083/wx/cart/index
     *
     * @return
     * @Author 12-江涛
     * @Date  2022/10/13 23:41
     */
    @GetMapping("index")
    public BaseRespVo index() {

        CartIndexVO cartIndexVO = cartService.cartIndex();
        if (cartIndexVO == null) {
            return BaseRespVo.couponError(10000, "请先登录商城");
        }
        return BaseRespVo.ok(cartIndexVO);
    }


    /*
     * 勾选与取消 (购物车商品 )
     * http://182.92.235.201:8083/wx/cart/checked
     *
     * @return
     * @Author 12-江涛
     * @Date  2022/10/13 23:41
     */
    @PostMapping("checked")
    public BaseRespVo checked(@RequestBody CartCheckedBO cartCheckedBO) {

        CartIndexVO cartIndexVO = cartService.cartChecked(cartCheckedBO);

        if (cartIndexVO == null) {
            return BaseRespVo.couponError(10000, "请先登录商城");
        }
        return BaseRespVo.ok(cartIndexVO);
    }


    /*
     * 加入购物车
     * http://182.92.235.201:8083/wx/cart/add
     *
     * @return
     * @Author 12-江涛
     * @Date  2022/10/13 23:41
     */
    @PostMapping("add")
    public BaseRespVo add(@RequestBody CartAddBO cartAddBO) {

        int count = cartService.cartAdd(cartAddBO);

        if (count == 10000) {
            return BaseRespVo.isParameter("当前用户拥挤，请重新操作！");
        } else if (count == 0) {
            return BaseRespVo.couponError(10000, "请先登录商城");
        } else {
            return BaseRespVo.ok(count);
        }
    }


    /*
     * 快速加入购物车
     * http://182.92.235.201:8083/wx/cart/fastadd
     *
     * @return
     * @Author 12-江涛
     * @Date  2022/10/14 19:57
     */
    @PostMapping("fastadd")
    public BaseRespVo fastadd(@RequestBody CartAddBO cartAddBO) {

        int count = cartService.fastadd(cartAddBO);

        if (count == 0) {
            return BaseRespVo.couponError(10000, "请先登录商城");
        } else if (count == 10000) {
            return BaseRespVo.isParameter("当前用户拥挤，请重新操作！");
        } else {
            return BaseRespVo.ok(count);
        }
    }

    /*
     * 编辑购物车中商品
     * http://182.92.235.201:8083/wx/cart/update
     *
     * @return
     * @Author 12-江涛
     * @Date  2022/10/14 13:54
     */
    @PostMapping("update")
    public BaseRespVo update(@RequestBody CartUpdateBO cartUpdateBO) {

        int count = cartService.cartUpdate(cartUpdateBO);

        if (count == 1) {
            return BaseRespVo.ok();
        } else if (count == 0) {
            return BaseRespVo.couponError(10000, "库存不足！");
        }else {
            return BaseRespVo.isParameter("操作有误！");
        }
    }

    /*
     * 删除购物车中商品
     * http://182.92.235.201:8083/wx/cart/delete
     *
     * @return
     * @Author 12-江涛
     * @Date  2022/10/14 19:19
     */
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody CartDeleteBO cartDeleteBO) {

        CartIndexVO cartIndexVO = cartService.cartDelete(cartDeleteBO.getProductIds());

        if (cartIndexVO != null) {
            return BaseRespVo.ok(cartIndexVO);
        }
        return BaseRespVo.isParameter("删除失败！");
    }


    /*
     * 显示数量，购物车（小气泡）
     * http://182.92.235.201:8083/wx/cart/goodscount
     *
     * @return
     * @Author 12-江涛
     * @Date  2022/10/15 14:24
     */
    @GetMapping("goodscount")
    public BaseRespVo goodscount() {

        long count = cartService.goodscount();

        if (count == -1) {
            //未登录
            return BaseRespVo.ok(0);
        }else {
            return BaseRespVo.ok(count);
        }
    }

    /*
     * 立即购买
     * http://182.92.235.201:8083/wx/cart/checkout
     *
     * @return
     * @Author 12-江涛
     * @Date  2022/10/14 16:21
     */
    @GetMapping("checkout")
    public BaseRespVo checkout(CartCheckoutBO cartCheckoutBO) {

        CartCheckoutVO cartCheckoutVO = cartService.cartCheckout(cartCheckoutBO);
        if (cartCheckoutVO == null) {
           return BaseRespVo.isParameter("当前用户拥挤，请重新购买");
        } else if (cartCheckoutVO.getCheckedAddress() == null) {
           return BaseRespVo.isParameter("请添加收获地址");
        }
        return BaseRespVo.ok(cartCheckoutVO);

    }

}
